package com.itheima.tanhua.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.autoconfig.template.HuanXinTemplate;
import com.itheima.tanhua.api.db.UserInfoServiceApi;
import com.itheima.tanhua.api.db.UserServiceApi;
import com.itheima.tanhua.api.mongo.CommentApi;
import com.itheima.tanhua.api.mongo.FriendServiceApi;
import com.itheima.tanhua.api.mongo.MessagesServiceApi;
import com.itheima.tanhua.exception.ConsumertException;
import com.itheima.tanhua.utils.Constants;
import com.itheima.tanhuan.CommentType;
import com.itheima.tanhuan.pojo.db.Friend;
import com.itheima.tanhuan.pojo.db.User;
import com.itheima.tanhuan.pojo.db.UserInfo;
import com.itheima.tanhuan.pojo.mongo.Comment;
import com.itheima.tanhuan.vo.db.ContactVo;
import com.itheima.tanhuan.vo.mongo.CommentVo;
import com.itheima.tanhuan.vo.mongo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @Autowired
    StringRedisTemplate redisTemplate;
    @DubboReference
    UserServiceApi userServiceApi;
    @DubboReference
    UserInfoServiceApi userInfoServiceApi;
    @DubboReference
    MessagesServiceApi messagesServiceApi;
    @Autowired
    HuanXinTemplate huanXinTemplate;
    @DubboReference
    FriendServiceApi friendServiceApi;
    @DubboReference
    CommentApi commentApi;
    //联系人列表
    public PageResult contacts(Integer page, Integer pageSize, String keyWord) {
        //1.用户id
        String d = redisTemplate.opsForValue().get("UID");
        Long uid = Convert.toLong(d);
       List<Friend>list=messagesServiceApi.findFriendsById(uid,page,pageSize,keyWord);
        //2.朋友集合id
        List<Long> friendIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        UserInfo userInfo=new UserInfo();
        userInfo.setNickname(keyWord);
        List<UserInfo> userInfoList = userInfoServiceApi.findByFriendIds(friendIds,keyWord);
        //3.朋友信息集合
        List<ContactVo>contactVos=new ArrayList<>();
        for (UserInfo userInfo1 : userInfoList) {
            ContactVo init = ContactVo.init(userInfo1);
            contactVos.add(init);
        }

        return new PageResult(page,pageSize,userInfoList.size(),userInfoList);
    }



    public void contactsAdd(Long friendId) {
        //添加好友
        Long uid = Convert.toLong(redisTemplate.opsForValue().get("UID"));

        //看他们是不是好友
        Boolean flag=friendServiceApi.isFriend(friendId,uid);
        if (!flag){
            //保存到好友数据库中
            //huanxi交友
            Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + friendId, Constants.HX_USER_PREFIX + uid);
            if (!aBoolean){
                throw new ConsumertException("你们已经是好友了");
            }
          friendServiceApi.createFriend(uid,friendId);
        }else { throw new ConsumertException("你们已经是好友了");}
    }
//评论列表
    public PageResult comments(Long page, Long pagesize) {
        //个人信息
        Long uid = Convert.toLong(redisTemplate.opsForValue().get("UID"));
        UserInfo userInfo = userInfoServiceApi.findById(uid);
        //个人评论
        List<Comment> commentList = commentApi.findByUserId(uid, CommentType.COMMENT,page,pagesize);
        List<CommentVo>commentVoList=new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVo init = CommentVo.init(userInfo, comment);
            commentVoList.add(init);
        }
        return new PageResult(Convert.toInt(page),Convert.toInt(pagesize),commentVoList.size(),commentVoList);
    }


}
